<template>
  <div>
    <el-table :data="tableData">
      <el-table-column
        label="一级指标"
        prop="firstLevelPoint"
      ></el-table-column>
      <el-table-column
        label="二级指标"
        prop="secondLevelPoint"
      ></el-table-column>
      <el-table-column label="改进建议" prop="improveMethod"></el-table-column>
      <el-table-column label="整改内容">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="getCertInfo(scope.row.id, true)"
            v-if="!scope.row.correctStatus"
            >填写</el-button
          >
          <el-button
            type="text"
            @click="getCertInfo(scope.row.id, true)"
            v-if="scope.row.correctStatus"
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="getCertInfo(scope.row.id, false)"
            v-if="scope.row.correctStatus"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="tableData.length > 0"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="page.pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="page.pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog :visible.sync="dialogVisible">
      <el-form label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="整改说明" prop="correctExplain">
          <el-col :span="10">
            <el-input v-model="formData.correctExplain" :disabled="!isEdit"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="上传内容" prop="content">
          <ossUpload @setCertAttachmentId="setCertAttachmentId" ref="ossRef" :isdisabled="!isEdit"></ossUpload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="fillIn">
          提 交
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ossUpload from "../../../components/ossUpload.vue"
export default {
  components: {
    ossUpload
  },
  data() {
    return {
      tableData: [
        {
          firstLevelPoint: "", // 一级指标
          secondLevelPoint: "", // 二级指标
          improveMethod: "" // 建议
        }
      ],
      formData: {
        correctExplain: "",
        certAttachmentId: 1
      },
      page: {
        pageNum: 1,
        pageSize: 10
      },
      dialogVisible: false,
      isEdit: false,
      isSubmit: false,
      rules: {
        correctExplain: [{ required: true, message: '请输入学校名称', trigger: 'blur' }],
      },
      totalPage: 0
    };
  },
  created() {
    this.getCert();
  },
  methods: {
    // 每页数
      sizeChangeHandle (val) {
        this.page.pageSize = val
        this.page.pageNum = 1
        this.getCert()
      },
      // 当前页
      currentChangeHandle (val) {
        this.page.pageNum = val
        this.getCert()
      },
    setCertAttachmentId(val) {
      this.formData.certAttachmentId = val
    },
    cancel() {
      Object.keys(this.formData).forEach(key => (this.formData[key] = ""));
      this.dialogVisible = false;
    },
    // 填写
    fillIn() {
      this.dialogVisible = true;
      if (!this.isSubmit) {
        this.$http({
          url: this.$http.cert("/improve"),
          method: "post",
          data: this.$http.adornData(
            {
              correctExplain: this.formData.correctExplain,
              proId: this.formData.id,
              certAttachmentId: this.formData.certAttachmentId
            },
            false
          )
        })
          .then(() => {
            this.getCert();
            this.$message.success("填写成功");
            this.$nextTick(()=> {
              this.$refs.ossRef.clearFiles()
           })
            this.dialogVisible = false;
          })
          .catch(() => {
            this.$message.error("填写失败");
          });
      } else {
        this.$http({
          url: this.$http.cert("/improve"),
          method: "put",
          data: this.$http.adornData(
            {
              correctExplain: this.formData.correctExplain,
              proId: id,
              certAttachmentId: this.formData.certAttachmentId
            },
            false
          )
        })
          .then(() => {
            this.getCert();
            this.$message.success("修改成功");
            this.dialogVisible = false;
          })
          .catch(() => {
            this.$message.error("修改失败");
          });
      }
    },
    // 查看详情
    getCertInfo(id, val) {
      this.isEdit = val;
      this.isSubmit = !val
      this.$http({
        url: this.$http.cert("/improve"),
        method: "get",
        params: this.$http.adornParams(
          {
            proId: id
          },
          false
        )
      }).then(res => {
        this.formData = res.data.data;
        this.$nextTick(()=> {
          this.$refs.ossRef.getUrl(this.formData.correctCertAttachmentId)
        })
        this.dialogVisible = true;
      });
    },
    // 获取该页数据
    getCert() {
      this.$http({
        url: this.$http.cert("/improvePage"),
        method: "get",
        params: this.$http.adornParams(
          {
            ...this.page,
            certId: this.$store.state.user.certId
          },
          false
        )
      }).then(res => {
        this.tableData = res.data.data.records;
        this.totalPage = res.data.data.total
      });
    }
  }
};
</script>

<style></style>
